package components
{
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.media.Camera;
	
	import mx.controls.Image;
	import mx.controls.VideoDisplay;
	import mx.graphics.ImageSnapshot;
	import mx.graphics.codec.JPEGEncoder;
	
	import spark.components.Button;
	import spark.components.supportClasses.SkinnableComponent;
	
	[SkinState("capturing")]
	public class PhotoInput extends SkinnableComponent
	{
		protected var camera:Camera;
		
		protected var isCameraOn:Boolean = false;
		
		[SkinPart(required="true")]
		public var videoDisplay:VideoDisplay;
		
		[SkinPart(required="true")]
		public var image:Image;
		
		[SkinPart(required="false")]
		public var takePictureButton:Button;
		
		[SkinPart(required="false")]
		public var startCameraButton:Button;
		
		[SkinPart(required="false")]
		public var stopCameraButton:Button;
		
		[Bindable]
		public var imageWidth:Number;
		
		[Bindable]
		public var imageHeight:Number;
		
		public function set source(source:Object):void
		{
			image.source = source;
		}
		
		public function get source():Object
		{
			return image.source;
		}
		
		public function PhotoInput()
		{
			super();
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if (instance == startCameraButton)
			{
				startCameraButton.addEventListener(MouseEvent.CLICK, startCameraButton_click);
			}
			if (instance == stopCameraButton)
			{
				stopCameraButton.addEventListener(MouseEvent.CLICK, stopCameraButton_click);
			}
			if (instance == takePictureButton)
			{
				takePictureButton.addEventListener(MouseEvent.CLICK, takePictureButton_click);
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			
			if (instance == startCameraButton)
			{
				startCameraButton.removeEventListener(MouseEvent.CLICK, startCameraButton_click);
			}
			if (instance == stopCameraButton)
			{
				stopCameraButton.removeEventListener(MouseEvent.CLICK, stopCameraButton_click);
			}
			if (instance == takePictureButton)
			{
				takePictureButton.removeEventListener(MouseEvent.CLICK, takePictureButton_click);
			}
		}		
		
		override protected function getCurrentSkinState():String
		{
			if (isCameraOn)
			{
				return "capturing";
			}
			
			return "normal";
		}
		
		protected function startCamera():void
		{
			isCameraOn = true;
			invalidateSkinState();
			callLater(attachCamera);
		}
		
		protected function attachCamera():void
		{
			camera = Camera.getCamera();
			videoDisplay.attachCamera(camera);
		}
		
		protected function stopCamera():void
		{
			isCameraOn = false;
			invalidateSkinState();
			videoDisplay.attachCamera(null);
		}
		
		protected function takePicture():void
		{
			var bd:BitmapData = ImageSnapshot.captureBitmapData(videoDisplay);
			var encoder:JPEGEncoder = new JPEGEncoder(80);
			image.source = encoder.encode(bd);			
		}		
		
		protected function startCameraButton_click(event:MouseEvent):void
		{
			startCamera();
		}
		
		protected function stopCameraButton_click(event:MouseEvent):void
		{
			stopCamera();
		}
		
		protected function takePictureButton_click(event:MouseEvent):void
		{
			takePicture();
		}
		
	}
}